package myapp.camera;

import java.util.ArrayList;

import myapp.DTO.CategoryDTO;
import myapp.dictionary.MyDictionary;
import myapp.googlemap.R;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

public class CategoryAdapter  extends ArrayAdapter<CategoryDTO> {
	Context context;
    int layoutResourceId;   
    ArrayList<CategoryDTO> data = null;
    int min = 0;
    int max = 0;
    public ArrayList<Boolean> bool_checkBox_selection;
    Gallery gallery_category;
    
	@SuppressWarnings("unchecked")
	public CategoryAdapter(Context context, int textViewResourceId, ArrayList<CategoryDTO> category_data, Gallery gallery) {
		super(context, textViewResourceId, category_data);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.layoutResourceId = textViewResourceId;
		this.data = (ArrayList<CategoryDTO>) category_data.clone();
		min = 0;
		max = category_data.size()-1;
		bool_checkBox_selection = new ArrayList<Boolean>();
		for (int i = 0; i < category_data.size(); i++)
		{
			bool_checkBox_selection.add(false);
		}
		gallery_category = gallery;
		
	}
	
	 @Override
	 public View getView(final int position, View convertView, ViewGroup parent) {
		 	View row = convertView;
		 	CategoryHolder holder = null;
	       
	        if(row == null)
	        {
	            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
	            row = inflater.inflate(layoutResourceId, parent, false);
	           
	            holder = new CategoryHolder();
	            holder.checkBox_selection = (CheckBox)row.findViewById(R.id.checkBox_category_camera);
	            holder.imgIconCategory = (ImageView)row.findViewById(R.id.imgCategoryIcon);
	            holder.txtname = (TextView)row.findViewById(R.id.text_view_CategoryName);
	            holder.imgIcon_previous = (ImageView)row.findViewById(R.id.imageView_previous_category_camera);
	            holder.imgIcon_next = (ImageView)row.findViewById(R.id.imageView_next_category_camera);
	            if (position == min)
	            	holder.imgIcon_previous.setAlpha(20);
	            if (position == max)
	            	holder.imgIcon_next.setAlpha(20);
	            
	            
	            row.setTag(holder);
	        }
	        else
	        {
	            holder = (CategoryHolder)row.getTag();
	        }
	       
	        CategoryDTO category = data.get(position);
	        holder.txtname.setText(category.getName());
	        holder.imgIconCategory.setImageResource(MyDictionary.dictionary.get(category.getId(), 0));
	        holder.checkBox_selection.setClickable(false);
/*	        holder.checkBox_selection.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					CheckBox cb = (CheckBox) v;
					int id = cb.getId();
					if (bool_checkBox_selection.get(id)){
						cb.setChecked(false);
						bool_checkBox_selection.set(id, false);
					} else {
						cb.setChecked(true);
						bool_checkBox_selection.set(id, true);
					}
				}
			});*/
	        holder.checkBox_selection.setChecked(bool_checkBox_selection.get(position));
	        
			holder.imgIcon_next.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (position != max)
					{
						
						gallery_category.setSelection(position + 1, true);
					}
				}
			});
			holder.imgIcon_previous.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (position != min)
					{
						gallery_category.onFling(null, null, 1f, 0f);
						gallery_category.setSelection(position - 1, true);
					}
				}
			});
			
	        return row;
	    }
	   
	    static class CategoryHolder
	    {
	    	CheckBox checkBox_selection;
	        ImageView imgIconCategory;
	        TextView txtname;
	        ImageView imgIcon_previous;
	        ImageView imgIcon_next;
	    }

}
